
function Tank(x ,y ,direct,color) {
	this.x = x;
	this.y = y;
	this.w = 30;
	this.h = 30;
	// 0 : 上 
	// 1 : 右 
	// 2 : 下
	// 3 : 左
	this.direct = direct;

	this.color = color;

	this.speed = 5;
	this.bullet= null;
	this.isShot= false;	
}
// Tank.prototype.speed = 5;
// Tank.prototype.bullet= null;
// Tank.prototype.isShot= false;
Tank.prototype.moveUp = function() {
	this.y -= 2;
	// 边界判断
	if ( this.y < 0 ) this.y = 0;
	// 砖块判断
	for (var i = 0 ; i < bricks.length; i++) {
		if ( checkPZ( this , bricks[i] ) ){
			this.y += 2;
		}
	}
	for (var i = 0 ; i < steels.length; i++) {
		if ( checkPZ( this , steels[i] ) ){
			this.y += 2;
		}
	}
	this.direct = 0;
};

Tank.prototype.moveRight = function() {
	this.x += 2;
	if ( this.x > 500 - 30 ) this.x = 500 - 30;
	// 砖块判断
	for (var i = 0 ; i < bricks.length; i++) {
		if ( checkPZ( this , bricks[i] ) ){
			this.x -= 2;
		}
	}
	// 钢板判断
	for (var i = 0 ; i < steels.length; i++) {
		if ( checkPZ( this , steels[i] ) ){
			this.x -= 2;
		}
	}
	this.direct = 1;
};

Tank.prototype.moveDown = function() {
	this.y += 2;
	if ( this.y > 500 - 30 ) this.y = 500 - 30;
	for (var i = 0 ; i < bricks.length; i++) {
		if ( checkPZ( this , bricks[i] ) ){
			this.y -= 2;
		}
	}
	for (var i = 0 ; i < steels.length; i++) {
		if ( checkPZ( this , steels[i] ) ){
			this.y -= 2;
		}
	}
	this.direct = 2;
};

Tank.prototype.moveLeft = function() {
	this.x -= 2;
	if ( this.x < 0 ) this.x = 0;
	for (var i = 0 ; i < bricks.length; i++) {
		if ( checkPZ( this , bricks[i] ) ){
			this.x += 2;
		}
	}
	for (var i = 0 ; i < steels.length; i++) {
		if ( checkPZ( this , steels[i] ) ){
			this.x += 2;
		}
	}
	this.direct = 3;
};





/**
 * 子弹类
 * 子弹坐标和方向
 * @param {Object} x
 * @param {Object} y
 * @param {Object} direct
 */
function Bullet(x,y,direct){
	this.x = x;
	this.y = y;
	this.w = 1;
	this.h = 1;
	this.direct = direct;

	this.speed = 3;
}


// 子弹击中坦克判断
function pz(tank , bullet) {
	tankWidth = 30 ;
	tankHeight= 30 ; 

	if ( bullet.x <= tank.x + 30 && bullet.x >= tank.x && 
			 bullet.y <= tank.y + 30 && bullet.y >= tank.y )
	{
		return true;
	}
	return false;

}
// 通用碰撞检测
function checkPZ( tank , brick){  //碰撞检测
	if( !tank || !brick ){
		return false;
	}
	var L1 = tank.x;
	var R1 = tank.x + tank.w;
	var T1 = tank.y;
	var B1 = tank.y + tank.h;
	
	var L2 = brick.x
	var R2 = brick.x + brick.w;
	var T2 = brick.y ;
	var B2 = brick.y + brick.h;
	
	if( R1<=L2 || L1>=R2 || T1>=B2 || B1<=T2 ){
		return false;
	}
	else{
		return true;
	}
}